<?php

require_once("../conn.php");

$total = 20;
if (isset($_GET['module'])) {
    $module = $_GET['module'];
    $codes = $_POST['code'];    
    $nums = $_POST['num'];

    $db = new DBC();
    if ($module == "suggest") {        
        $sql = "DELETE FROM t_market_suggest";
        if ($db->execute($sql)) {
            for ($i = 0; $i < $total; $i++) {
                if ($codes[$i] != "") {
                    $sql = "SELECT flashcode,name FROM t_product WHERE flashcode = '" . $codes[$i] . "'";
                    $ps = $db->fetchData($sql);
                    if ($ps != false && count($ps) > 0) {
                        $code = $ps[0]['flashcode'];
                        $name = $ps[0]['name'];
                        $sql = "INSERT INTO t_market_suggest(code,name,num) VALUES('" . $code . "','" . $name . "'," . $nums[$i] . ")";
                        $db->execute($sql);
                    }
                }
            }
        }
        $transfer = "marketSuggest.php?method=modify&menu=suggest";
    } else if ($module == "download") {        
            $sql = "DELETE FROM t_market_downloadTimes";
            if ($db->execute($sql)) {
                for ($i = 0; $i < $total; $i++) {
                    if ($codes[$i] != "") {
                        $sql = "SELECT flashcode,name,downloadtimes FROM t_product WHERE flashcode = '" . $codes[$i] . "'";
                        $ps = $db->fetchData($sql);
                        if ($ps != false && count($ps) > 0) {
                            $code = $ps[0]['flashcode'];
                            $name = $ps[0]['name'];
                            $downloadtimes = $ps[0]['downloadtimes'];
                            $sql = "INSERT INTO t_market_downloadTimes(code,name,downloadTimes,num) VALUES('" . $code . "','" . $name . "'," . $downloadtimes . "," . $nums[$i] . ")";
                            $db->execute($sql);
                        }
                    }
                }
            }
        
        $transfer = "marketDownload.php?method=modify&menu=download";
    } else if ($module == "uploadtime") {
        $sql = "DELETE FROM t_market_uploadtime";
        if ($db->execute($sql)) {
            for ($i = 0; $i < $total; $i++) {
                if ($codes[$i] != "") {
                    $sql = "SELECT flashcode,name,createdate FROM t_product WHERE flashcode = '" . $codes[$i] . "'";
                    $ps = $db->fetchData($sql);
                    if ($ps != false && count($ps) > 0) {
                        $code = $ps[0]['flashcode'];
                        $name = $ps[0]['name'];
                        $uploadtime = $ps[0]['createdate'];
                        $sql = "INSERT INTO t_market_uploadtime(code,name,uploadTime,num) VALUES('" . $code . "','" . $name . "','" . $uploadtime . "'," . $nums[$i] . ")";
                        $db->execute($sql);
                    }
                }
            }
        }
        $transfer = "marketUpload.php?method=modify&menu=uploadtime";
    } else if ($module == "all") {
        $page = $_POST['page'];
        for ($i = 0; $i < $total; $i++) {
            if ($codes[$i] != "") {
                $sql = "UPDATE t_product SET num = '" . $nums[$i] . "' WHERE flashcode = '" . $codes[$i] . "'";
                $db->execute($sql);
            }
        }
        $transfer = "marketAll.php?method=modify&menu=all&page=" . $page;
    }
    echo "<script language='javascript'>location='" . $transfer . "';</script>";
} else {
    echo "<script language='javascript'>history.back();</script>";
}
?>
